package com.sun.springboot_mq1.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**延迟队列优化（安装延时队列插件）
 * 新增了一个队列 delayed.queue,一个自定义交换机 delayed.exchange
 * @author shkstart
 * @create 2021-10-25 12:43
 */
@Configuration
public class DelayedQueueConfig {
    public static final String Delayed_QUEUE="delayed.queue";
    public static final String Delayed_EXCHANGE="delayed.exchange";
    @Bean("delayed.queue")
    public Queue queueDelayed(){
        return new Queue(Delayed_QUEUE);
    }
   /* @Bean("delayed.exchange")
    public DirectExchange exchangeDelayed(){
        return new DirectExchange(Delayed_EXCHANGE);
    }*/
    // 自定义交换机 我们在这里定义的是一个延迟交换机
    @Bean("delayed.exchange")
    public CustomExchange delayedExchange(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange(Delayed_EXCHANGE,"x-delayed-message",true,false,arguments);
    }
    @Bean
    public Binding bindingDelayed(@Qualifier("delayed.queue") Queue queue,@Qualifier("delayed.exchange")CustomExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("delayed.routingKey").noargs();
    }
}
